<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客登陆页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">

</head>

<body>
    <div class="nav">
        <img src="pic/nuc_logo.jpg" alt="">
        <span class="blog-title">网页聊天系统</span>
        <div class="space"></div>
    </div>

    <div class="container-login">
        <div class="login-dialog">
            <h3>登陆</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" name="username" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" name="password" id="password">
            </div>
            <div class="row">
                <button id="submit" onclick="login()">提交</button>
            </div>

            <div class="row">
                <span>还没有账号？</span>
                <a href="chatroom_register.html" class="register-link">点击注册</a>
            </div>

        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        function login() {


            $.ajax({
                type: "post",
                url: "/user/login",
                contentType: "application/json",
                data: JSON.stringify({
                    username: $("#username").val(),  // 关键修改：参数名改为username
                    password: $("#password").val(),
                }),
                success: function (result) {
                    console.log("登录响应: ", result);
                    if (result != null && result.code == "SUCCESS") {
                        localStorage.setItem("userId", result.data.userId);
                        localStorage.setItem("username", result.data.username);
                        localStorage.setItem("token", result.data.token);
                        //alert("登陆成功");
                        location.href = "client.html";
                    } else {
                        alert(result.errMsg || "登录失败，请检查用户名和密码");
                    }
                },
                error: function (xhr, status, error) {
                    console.error("登录请求错误: ", error);
                    alert("登录请求失败，请稍后再试");
                }
            });
        }
    </script>
</body>

</html>